package com.course.business.controller.admin;

import com.course.server.domain.Chapter;
import com.course.server.dto.ChapterDTO;
import com.course.server.dto.PageDTO;
import com.course.server.dto.ResponseDTO;
import com.course.server.service.ChapterService;
import com.course.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/chapter")
public class ChapterController {

    private static final Logger logger = LoggerFactory.getLogger(ChapterController.class);

    public static final String BUSINESS_NAME = "大章";

    @Resource
    private ChapterService chapterService;

    @PostMapping("/chapter")
    public String chapter(){
        return "success";
    }

    @PostMapping("/list")
    public ResponseDTO list(@RequestBody PageDTO pageDTO){
        logger.info("pageDTO:{}", pageDTO);
        ResponseDTO responseDTO = new ResponseDTO();
        chapterService.list(pageDTO);
        responseDTO.setContent(pageDTO);
        return responseDTO;
    }

    @PostMapping("/save")
    public ResponseDTO save(@RequestBody ChapterDTO chapterDTO){
        logger.info("chapterDTO:{}", chapterDTO);

        //保存校验
        ValidatorUtil.require(chapterDTO.getName(), "名称");
        ValidatorUtil.require(chapterDTO.getCourseId(), "课程ID");
        ValidatorUtil.length(chapterDTO.getCourseId(), "课程ID", 1, 8);

        ResponseDTO responseDTO = new ResponseDTO<>();
        chapterService.save(chapterDTO);
        responseDTO.setContent(chapterDTO);
        return responseDTO;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDTO save(@PathVariable String id){
        logger.info("id:{}", id);
        ResponseDTO responseDTO = new ResponseDTO<>();
        chapterService.delete(id);
        return responseDTO;
    }
}
